package com.tutu.service.impl;

import com.tutu.controller.CommonsController;
import com.tutu.core.api.SystemApiUrls;
import com.tutu.service.RoleService;
import com.tutu.utils.BaseUtil;
import com.tutu.utils.HttpRequestUtil;
import com.tutu.utils.JsonUtil;
import com.tutu.utils.UnicodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * roleServiceImpl
 * Created by 奕恒 on 2017/2/20 21:54.
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {

    @Override
    public Map<String, Object> pageByParams(Integer pageNum, Integer pageSize) {
        return HttpRequestUtil.getOnlyData(SystemApiUrls.base_prefix + SystemApiUrls.role_page, "pageNum=" + pageNum + "&pageSize=" + pageSize);
    }

    @Override
    public Map<String, Object> getInfoById(Integer id) {
        return HttpRequestUtil.getCompleteData(SystemApiUrls.base_prefix + SystemApiUrls.role_info, "id=" + id);
    }

    @Override
    public Map<String, Object> saveInfo(HttpServletRequest request, Integer id, String name, String moduleIds) {
        LogInfoServiceImpl.saveLogInfo(request, "编辑角色信息，ID：" + id + "名称：" + name + "模块IDS：" + moduleIds);

        Map<String, Object> resultMap;
        String url = SystemApiUrls.base_prefix + SystemApiUrls.role_update;

        if (null == id) url = SystemApiUrls.base_prefix + SystemApiUrls.role_create;

        try {
            if (BaseUtil.isNotBlank(moduleIds)) {
                Map<String, Object> map = HttpRequestUtil.getCompleteData(url, "id=" + id + "&name=" + UnicodeUtil.unicodeEncode(name));

                if (!map.get("code").equals(0)) return BaseUtil.defaultErrorInfo(map.get("msg") + "");

                resultMap = HttpRequestUtil.getCompleteData(SystemApiUrls.base_prefix + SystemApiUrls.role_updateModuleRelation, "roleId=" + map.get("data") + "&moduleIds=" + moduleIds);
            } else {
                resultMap = HttpRequestUtil.getCompleteData(url, "id=" + id + "&name=" + UnicodeUtil.unicodeEncode(name));
            }

            loadCacheData();
        } catch (Exception e) {
            e.printStackTrace();

            resultMap = BaseUtil.defaultErrorInfo(null);
        }

        return resultMap;
    }

    @Override
    public Map<String, Object> deleteById(HttpServletRequest request, Integer id) {
        LogInfoServiceImpl.saveLogInfo(request, "删除角色信息，ID：" + id);

        Map<String, Object> map = HttpRequestUtil.getCompleteData(SystemApiUrls.base_prefix + SystemApiUrls.role_delete, "id=" + id);

        loadCacheData();

        return map;
    }

    @Override
    public Map<String, Object> batchDel(HttpServletRequest request, String ids) {
        LogInfoServiceImpl.saveLogInfo(request, "批量删除角色信息，IDS：" + ids);

        Map<String, Object> map = HttpRequestUtil.getCompleteData(SystemApiUrls.base_prefix + SystemApiUrls.role_batchDel, "ids=" + ids);

        loadCacheData();

        return map;
    }

    @Override
    public List<Integer> findMenuListByRoleId(Integer roleId) {
        List<Integer> list = new ArrayList<>();

        for (Object o : HttpRequestUtil.getListData(SystemApiUrls.base_prefix + SystemApiUrls.role_findModuleListByRoleId, "roleId=" + roleId))
            list.add(Integer.parseInt(o + ""));

        return list;
    }

    @Override
    public void loadCacheData() {
        CommonsController.baseRoleList.clear();

        for (Object o : HttpRequestUtil.getListData(SystemApiUrls.base_prefix + SystemApiUrls.role_list, ""))
            CommonsController.baseRoleList.add(JsonUtil.json2Map(o));

        if (CommonsController.baseRoleList.size() <= 1) {
            try {
                log.info("角色信息请求失败，3秒后自动重试...");

                Thread.sleep(3000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            loadCacheData();
        }

        log.info("共计加载" + CommonsController.baseRoleList.size() + "条角色信息...");
    }
}